
package com.seekting.study;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class GJsonTest {

    public static class Apple {

        private String name;
        private String age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }

    public static class Person {
        private String name;
        private int age;
        private List<Apple> items;
        private Object obj;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public List<Apple> getItems() {
            return items;
        }

        public void setItems(List<Apple> items) {
            this.items = items;
        }

        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", items=" + items + ", obj=" + obj
                    + "]";
        }

    }

    public static class AbstractPerson {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "AbstractPerson [name=" + name + "]";
        }
    }

    public static void main(String[] args) {

        Person person = new Person();
        person.age = 1;
        person.name = "jack";
        List<Apple> items = new ArrayList<Apple>();

        Apple apple = new Apple();
        apple.setName("aple");
        apple.setAge("2");
        items.add(apple);
        person.setItems(items);
        Gson gson = new Gson();
        String json = gson.toJson(person);
        System.out.println(json);
        Person person2 = gson.fromJson(json, Person.class);
        System.out.println(person2);

    }
}
